using NUnit.Framework;
using SmartPeople.Framework.SecuritySpace.Tests;

namespace SmartPeople.Framework.SecuritySpace.Provider.Tests
{
    [TestFixture]
    public class ISecuritySecuredDocumentsProviderTests
    {
        private void Init(ISecuritySpace securitySpace)
        {
            //securitySpace.Clear();

            //ISecuritySecuredType securedFile = securitySpace.SecuredTypes.CreateType("unknownFileType");
            //securedFile.Actions.CreateAction(TestActions.Read);
            //securedFile.Actions.CreateAction(TestActions.Create);
            //securedFile.Actions.CreateAction(TestActions.Modify);
            //securedFile.Actions.CreateAction(TestActions.Delete);

            //securedFile.States.CreateState(TestStates.New);
            //securedFile.States.CreateState(TestStates.Normal);
            //securedFile.States.CreateState(TestStates.Protected);

            ISecuritySecuredType baseSecuredProperty = securitySpace.SecuredTypes.CreateType("BaseProperty");
            baseSecuredProperty.Actions.CreateAction(TestActions.Get);
            baseSecuredProperty.Actions.CreateAction(TestActions.Set);


            //securitySpace.SecuredDocuments.SetDefaultPathType(securedFolder);
            ISecuritySecuredType securedFile =
                securitySpace.SecuredTypes.GetSecuredType(BuiltInSecuredTypes.UnknownFileType);
            securitySpace.SecuredDocuments.SetDefaultDocumentType(securedFile);

            securitySpace.Roles.CreateRole(TestRoles.Administrators);
            securitySpace.Roles.CreateRole(TestRoles.Operators);
            securitySpace.Roles.CreateRole(TestRoles.Users);

            securitySpace.Users.CreateUser(TestUsers.System, "system1", "system@acme.com");
            securitySpace.Users.CreateUser(TestUsers.Administrator, "admin1", "admin@acme.com");
            securitySpace.Users.CreateUser(TestUsers.Guest, "guest1", "guest@acme.com");
            securitySpace.Users.CreateUser(TestUsers.User1, "user1", "user1@acme.com");

        }

        private ISecuritySecuredDocumentsProvider GetSecuredDocuments()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            //securitySpace.Clear();
            Init(securitySpace);
            return securitySpace.SecuredDocuments;
        }

        private SecuritySecuredTypes GetSecuredTypes()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            return securitySpace.SecuredTypes;
        }


        [Test, Description("Test to clear security space,but the system intialized type n doc must remain")]
        public void ClearSecuritySpace()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();
            //buildin types = 2
            Assert.AreEqual(2, securitySpace.SecuredTypes.Count);
            Assert.AreEqual(1, securitySpace.SecuredDocuments.Count);
        }

        //SecDoc_001
        [Test]
        public void InitTests()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            //securitySpace.Clear();
            //Init(securitySpace);
            Assert.AreEqual(1, securitySpace.SecuredDocuments.Count);
            securitySpace.Clear();
        }

        //SecDoc_002
        [Test]
        public void CreatePathTests()
        {
            ISecuritySecuredDocumentsProvider securedDocuments = GetSecuredDocuments();
            //securedDocuments.Clear();
            //Root not managed by the provider
            Assert.AreEqual(1, securedDocuments.Count);

            ISecuritySecuredType securedFolder = GetSecuredTypes().GetSecuredType(BuiltInSecuredTypes.Folder);

            string salesPath = "/ACME/HRMS/OS/Departments/Sales";
            securedDocuments.CreatePath("/ACME/HRMS/Employees", securedFolder, securedFolder);
            securedDocuments.CreatePath(salesPath, securedFolder, securedFolder);

            //including root
            Assert.AreEqual(7, securedDocuments.Count);
            ISecuritySecuredDocument securedDocument;
            securedDocument = securedDocuments.GetDocument(salesPath);
            Assert.AreEqual(salesPath, securedDocument.FullName);
            Assert.AreEqual(BuiltInSecuredTypes.Folder, securedDocument.SecuredType.Name);
            
            securedDocuments.Clear();
        }

        //SecDoc_003
        [Test]
        public void GetKnownParentPathTests()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();
            Init(securitySpace);
            ISecuritySecuredDocument securedPath;
            string contactsPath = "/ACME/CRM/Contacts";
            securedPath = securitySpace.SecuredDocuments.CreatePath(contactsPath);
            ISecuritySecuredDocument securedPath1;
            securedPath1 = securitySpace.SecuredDocuments.GetKnownParentPath(contactsPath + "/*/Phones");
            Assert.AreEqual(securedPath.FullName, securedPath1.FullName);
            securitySpace.Clear();
        }

        [Test, Description("Check if provider methods work properly")]
        public void VerfiyPathCorrectType()
        {
            bool authorized = true;
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();
            //ISecuritySecuredType folderType = securitySpace.SecuredTypes.CreateType("folder");
            //folderType.Actions.CreateAction(TestActions.List);
            //folderType.Actions.CreateAction(TestActions.Create);
            //folderType.Actions.CreateAction(TestActions.Delete);
            //securitySpace.SecuredDocuments.SetDefaultPathType(folderType);

            //securitySpace.SecuredTypes.UpdateSecuredType(folderType);


            ISecuritySecuredType genderType = securitySpace.SecuredTypes.CreateType("genderType");
            genderType.Actions.CreateAction(TestActions.Create);
            genderType.Actions.CreateAction(TestActions.Delete);
            genderType.Actions.CreateAction(TestActions.List);
            securitySpace.SecuredDocuments.SetDefaultDocumentType(genderType);


            securitySpace.SecuredTypes.UpdateSecuredType(genderType);

            string genderPath = "/ACME/HRMS/TA/Gender";
            //ISecuritySecuredDocument securedPath = securitySpace.SecuredDocuments.CreatePath(commonPath, folderType);
            ISecuritySecuredType sectype = securitySpace.SecuredTypes.GetSecuredType("folder");
            ISecuritySecuredDocument securedPath = securitySpace.SecuredDocuments.CreatePath(genderPath, sectype);
            Assert.AreEqual(sectype.GID, securedPath.SecuredType.GID);

            ISecuritySecuredDocument securedDocument =
                securitySpace.SecuredDocuments.CreateDocument(genderPath, "Male1", "genderType");
            Assert.AreEqual(genderType.GID, securedDocument.SecuredType.GID);

            securitySpace.Clear();
        }


        [Test]
        public void UpdatePath()
        {
            bool authorized = true;
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();
            //ISecuritySecuredType folderType = securitySpace.SecuredTypes.CreateType("folder");
            //folderType.Actions.CreateAction(TestActions.List);
            //folderType.Actions.CreateAction(TestActions.Create);
            //folderType.Actions.CreateAction(TestActions.Delete);
            //securitySpace.SecuredDocuments.SetDefaultPathType(folderType);
            //securitySpace.SecuredTypes.UpdateSecuredType(folderType);
            ISecuritySecuredType genderType = securitySpace.SecuredTypes.CreateType("genderType");
            genderType.Actions.CreateAction(TestActions.Create);
            genderType.Actions.CreateAction(TestActions.Delete);
            genderType.Actions.CreateAction(TestActions.List);
            securitySpace.SecuredDocuments.SetDefaultDocumentType(genderType);
            securitySpace.SecuredTypes.UpdateSecuredType(genderType);

            string genderPath = "/ACME/HRMS/TA/Gender";
            ISecuritySecuredType sectype = securitySpace.SecuredTypes.GetSecuredType("folder");
            ISecuritySecuredDocument securedPath = securitySpace.SecuredDocuments.CreatePath(genderPath, sectype);
            Assert.AreEqual(sectype.GID, securedPath.SecuredType.GID);
            string updatedgenderPath = "/ACME/HRMS/TA/Genders";
            //ISecuritySecuredDocument securedPath = securitySpace.SecuredDocuments.UpdatePath()


            //ISecuritySecuredDocument securedDocument =
            //    securitySpace.SecuredDocuments.CreateDocument(genderPath, "Male1", "genderType");
            //Assert.AreEqual(genderType.GID, securedDocument.SecuredType.GID);

            securitySpace.Clear();
        }

        [Test, Description("Create multiple document.")]
        public void CreateMulipleDocumentTest()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();

            ISecuritySecuredType folderType = securitySpace.SecuredTypes.GetSecuredType(BuiltInSecuredTypes.Folder);
            ISecuritySecuredType employeeType = securitySpace.SecuredTypes.CreateType("Employee",BuiltInSecuredTypes.UnknownFileType);
            securitySpace.SecuredDocuments.SetDefaultDocumentType(employeeType);
            
            ISecuritySecuredType employeeCertificateType = securitySpace.SecuredTypes.CreateType("Employee Certificate", BuiltInSecuredTypes.UnknownFileType);
            
            ISecuritySecuredDocument employeesPath = securitySpace.SecuredDocuments.CreatePath("/ACME/HR/Employees");//, folderType, employeeType);
            ISecuritySecuredDocument employeeJohnPath =
                securitySpace.SecuredDocuments.CreateDocument(employeesPath.FullName,"John Smith",employeeType.Name);
            
            employeeJohnPath.DefaultChildSecuredType = employeeCertificateType;
            securitySpace.SecuredDocuments.UpdatePath(employeeJohnPath);

            ISecuritySecuredDocument employeeJohnCertPath =
                securitySpace.SecuredDocuments.CreateDocument(employeeJohnPath.FullName, "MCP");


            Assert.AreEqual(folderType.Name, employeesPath.SecuredType.Name);
            Assert.AreEqual(employeeType.Name, employeeJohnPath.SecuredType.Name);
            Assert.AreEqual(employeeCertificateType.Name, employeeJohnCertPath.SecuredType.Name);

            securitySpace.Clear();
        }
    }
}